home *** CD-ROM | disk | FTP | other *** search
/ NetNews Offline 2 / NetNews Offline Volume 2.iso / news / comp / lang / c-part2 / 15356 < prev    next >
Encoding:
Internet Message Format  |  1996-08-05  |  2.5 KB

  1. Path: lrz-muenchen.de!news
  2. From: watzka@stat.uni-muenchen.de (Kurt Watzka)
  3. Newsgroups: comp.lang.c
  4. Subject: Re: passing 2D arrays to functions
  5. Date: 18 Apr 1996 17:22:59 GMT
  6. Organization: Leibniz-Rechenzentrum, Muenchen (Germany)
  7. Distribution: world
  8. Message-ID: <4l5tpj$1eu@sparcserver.lrz-muenchen.de>
  9. References: <4l58sk$l6r@harbinger.cc.monash.edu.au>
  10. NNTP-Posting-Host: sun2.lrz-muenchen.de
  11.  
  12. pdrod1@mdw084.cc.monash.edu.au (Mr Paul Rodger) writes:
  13.  
  14. >Greets - 
  15.  
  16. >I need to pass a 2-dimensional array to a function. Obviously I just want
  17. >to pass a pointer to the first element, and in the past when I wanted to 
  18. >pass a one-dimensional array to a function I did the following which should 
  19. >work fine:
  20.  
  21. >fun(int array[])
  22. >{
  23. >...
  24. >}
  25.  
  26. >But when I try to pass a 2D array using the same method, slightly different:
  27.  
  28. >fun(int array[][])
  29. >{
  30. >...
  31. >}
  32.  
  33. This is more than slightly different from the above. "array" is a pointer
  34. to int in the first example, whereas it is a pointer to an array of
  35. unspecified size of int (i.e. an incomplete type) in the second example.
  36.  
  37. While there is nothing wrong with passing pointers to incomplete types
  38. in C, it is impossible to use an incomplete type to access elements or
  39. members of the complete type behind it.
  40.  
  41. >main()
  42. >{
  43. >        int array[10][10];
  44. >...
  45. >        fun(array);
  46. >}
  47.  
  48. >I'm getting errors:
  49.  
  50. >main.c:221: arithmetic on pointer to an incomplete type
  51. >make: *** [main.o] Error 1
  52.  
  53. >This occurs when I try to look at elements in the array when I am
  54. >in the 'fun' function:
  55.  
  56. >array[1][1] = 1;
  57.  
  58. >Would this be because of vagueness about row-major or column-major form?
  59.  
  60. No, certainly not. It is because in C, an array of unspecified size is an
  61. incomplete type.
  62.  
  63. >I've got around this in the past by putting the 2D array in a struct,
  64. >and passing a pointer to a struct, but in this case I can't do that 
  65. >because the array size could vary as it is declared various times in 
  66. >iterations. (It isn't declared in main like above - this is just a 
  67. >simplication).
  68.  
  69. The FAQ explains one method for handling two-dimensional dynamic arrays.
  70. If you need only the first dimension of the array to be dependent on
  71. dynamic information, try
  72.  
  73.    int fun(int (* array)[10]);
  74.  
  75. You can pass an array of N arrays of 10 ints to "fun()", where N can
  76. be different in different invocations of the function. (You will have
  77. to pass the actual size of the array as an additional parameter).
  78.  
  79. Kurt
  80. --
  81. | Kurt Watzka                             Phone : +49-89-2180-6254
  82. | watzka@stat.uni-muenchen.de
  83.